package edu.wpi.cs509.ui;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import edu.wpi.cs509.representation.PlayerPiece;

/**
 * The mouse listener that handles mouse events occurring in the graveyard panel.
 * Each graveyard plot is enabled to have a mouse listener to allow the player to 
 * select a piece during initial placement.
 * 
 * @author afoltan
 *
 */
public class GraveyardMouseListener implements MouseListener {

	// the graveyard plot which will notify mouse events to this listener
	private GraveyardPlot plot; 
	
	// the event notification interface for this listener to notify
	// the stratego application when mouse events occur
	private GraveyardNotify notify;
	
	GraveyardMouseListener( GraveyardPlot plot, GraveyardNotify notify ) {
		this.plot = plot;
		this.notify = notify;
	}
   	 
	/**
	 * This method is called when the mouse is clicked within the
	 * graveyard plot container (graphic or text label)
	 * 
	 * The handling of the mouse click first will check whether the piece
	 * has a positive count available, and if not, the "No Piece" dialog will
	 * automatically be shown.
	 * 
	 * If a piece is available, it is selected (highlighted), and the application
	 * event handler is notified.
	 * 
	 * @param e - the mouse event object
	 */
    @Override
    public void mouseClicked(MouseEvent e) {
    	System.out.print("click");
    
    	// if this is opaque graveyard, no notifications are sent. 
    	if (notify == null) {
    		return;
    	}
    	
		plot.getGraveyard().unselectAll();
		
    	// highlight the graveyard plot
    	if (plot.isSelected()) {
    		plot.setSelected(false);
    	} else {
    		plot.setSelected(true);
    	}
    	
    	// notify the event handler
    	notify.pieceSelected( plot.getGraveyard(), new PlayerPiece(plot.getStrategoPiece(), plot.getPlayerColor() ));
    }

    /**
     * Indicates the mouse has entered the graveyard plot container.
     * @param e - the mouse event object
     */
	@Override
	public void mouseEntered(MouseEvent e) {
		// currently not used
	}

    /**
     * Indicates the mouse has exited the graveyard plot container.
     * @param e - the mouse event object
     */
	@Override
	public void mouseExited(MouseEvent e) {
		// currently not used
	}

    /**
     * Indicates the mouse has been pressed in the graveyard plot container.
     * @param e - the mouse event object
     */
	@Override
	public void mousePressed(MouseEvent e) {
		// currently not used
	}

    /**
     * Indicates the mouse has been release in the graveyard plot container.
     * @param e - the mouse event object
     */
	@Override
	public void mouseReleased(MouseEvent e) {
		// currently not used
	}

}
